<?php
namespace Admin\Controller;

use Common\Controller\AuthController;


class IndexController extends AuthController
{
    //后台首页
    public function index()
    {

        $robj = M('Orders');
        $re['number_1'] = $robj->where("status=%d", 0)->count();//待审核
        $re['number_2'] = $robj->where("status=%d", 3)->count();//待确认
        $re['number_3'] = $robj->where("status=%d", 1)->count();//生产中
        $re['number_4'] = $robj->where("status=%d", 2)->count();//生产中
        $counts = '';
        foreach ($re as $v) {
            $counts += $v;
        }
        $proportion = [];
        foreach ($re as $key => $val) {
            $proportion[$key] = round(($val / $counts * 100), 2) . '%';
        }

        //查询统计数据
        $year=intval(Date("Y"));//当前年份
        $month=intval(Date("m"));//当前月份
        $datas=M('Statistics')->field("mid,ordernumber,orderprice,stime")->where("yid=%d",$year)->select();

        $stime=M('Statistics')->where("yid=%d and mid=%d",[$year,$month])->getField('stime');
        $revs=[];
        foreach($datas as $v){
                $revs[$v['mid']]=$v;
        }
        $shuju=[];
        for($i=1;$i<=$month;$i++){
                if($revs[$i]){
                    $shuju[$i]['ordernumber']=$revs[$i]['ordernumber'];
                    $shuju[$i]['orderprice']=$revs[$i]['orderprice'];
                }else{
                    $shuju[$i]['ordernumber']=0;
                    $shuju[$i]['orderprice']=0;
                }

        }

        $this->assign('stime',date('Y-m-d H:i:s',$stime));

        $this->assign('datas',json_encode($shuju));
        $this->assign('props', $proportion);
        $this->assign('counts', $counts);
        $arr = D('Inform')->messageList();
        $this->assign('re', $re);
        $this->assign('_list', $arr['list']);
        $this->assign('_page', $arr['page']->show());
        $this->assign('title', '后台管理首页');
        $this->display();
    }

    public function isRead()
    {

        //更新通知消息
        if (isset($_GET['s_id']) && !empty($_GET['s_id'])) {
            $s_id = $_GET['s_id'];
            M('Inform')->where("id=%d", $s_id)->save(['is_read' => 1]);

        }
    }

    //删除通知信息
    public function infodel()
    {

        if (IS_AJAX) {
            $id = $_GET['id'];
            if (M('Inform')->where("id=%d", $id)->delete()) {
                $this->success('删除成功', '', IS_AJAX);
            } else {
                $this->error('删除失败', '', IS_AJAX);
            }
        }
    }



    //写入统计数据
    public function tongji()
    {

        $year=intval(Date("Y"));//当前年份

        $month=intval(Date('m'));//当前月份
        $one=2678400;//一月的时间戳
        $dqMonth=strtotime(date('Y-m '));//当前月份时间戳
        $nextMonth=$dqMonth+$one;//下月时间
        $condition['order_time'] = array(between,array($dqMonth,$nextMonth));
        $condition['status']=array('gt',0);
        $re=M('Orders')->field("rid,order_price")->where($condition)->select();
        $orderNumber=count($re);
        $orderPrice=0;
        foreach($re as $v){
            $orderPrice+=$v['order_price'];
        }
        $data['yid']=$year;
        $data['mid']= $month;
        $data['orderNumber']=$orderNumber;
        $data['orderPrice']=$orderPrice;
        $data['stime']=time();

        $filed=M('Statistics')->where("yid=%d and mid=%d",[$year,$month])->find();
        if($filed){
           $arr= M('Statistics')->where("yid=%d and mid=%d",[$year,$month])->save($data);
        }else{
            $arr=M('Statistics')->add($data);

        }
        if($arr){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }

    }

}













